<template>
  <div class="app-container">
    <!-- 查询表单 -->
    <el-form :model="queryParams" ref="queryRef" :inline="true">
      <el-form-item label="电表编号" prop="dbbh">
        <el-input
          v-model="queryParams.dbbh"
          placeholder="请输入电表编号"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="租户" prop="qymc">
        <el-input
          v-model="queryParams.qymc"
          placeholder="请输入租户"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- Tab 栏 -->
    <el-tabs v-model="activeTab" @tab-click="handleTabChange">
      <el-tab-pane label="租用中" name="used"></el-tab-pane>
      <el-tab-pane label="已退用" name="free"></el-tab-pane>
    </el-tabs>

    <!-- 数据表格 -->
    <el-table
      v-loading="loading"
      :data="tableData" 
    >
      <el-table-column prop="dbbh" label="电表编号" align="center" />
      <el-table-column prop="zt" label="载体" align="center" />
      <el-table-column prop="fjh" label="房间号" align="center" />
      <el-table-column prop="qymc" label="租户" align="center" width="350" show-overflow-tooltip/>

      <el-table-column prop="dbyt" label="电表用途" align="center" />
      <el-table-column prop="qysj" label="启用时间" align="center" />
      <el-table-column prop="csdl" label="初始电量" align="center" />
      <el-table-column prop="dbbl" label="电表倍率" align="center" />
      <el-table-column
        v-if="activeTab === 'free'"
        prop="tysj"
        label="退用时间"
        align="center"
      />
      <el-table-column
        v-if="activeTab === 'free'"
        prop="zzdl"
        label="最终电量"
        align="center"
      />
    </el-table>

    <!-- 分页 -->
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script setup>
import { ref, reactive, toRefs } from "vue";
import { selectUseList, selectFreeList } from "@/api/carrier/ammeter";

const activeTab = ref("used");
const tableData = ref([]);
const loading = ref(false);
const total = ref(0);

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    dbbh: undefined,
    qymc: undefined,
  },
});
const { queryParams } = toRefs(data);

/** 查询列表 */
function getList() {
  loading.value = true;
  const api = activeTab.value === "used" ? selectUseList : selectFreeList;
  api(queryParams.value).then((res) => {
    tableData.value = res.data?.list || [];
    total.value = res.data?.total || 0;
    loading.value = false;
  }).catch(() => {
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  queryParams.value = {
    pageNum: 1,
    pageSize: 10,
    dbbh: undefined,
    qymc: undefined,
  };
  getList();
}

/** Tab 切换 */
function handleTabChange(tab) {
  activeTab.value = tab.props.name;
  queryParams.value.pageNum = 1;
  getList();
}

getList();
</script>

<style scoped>
.app-container {
  padding: 20px;
}
</style>